Editing a PDF in Vim
published 9 Nov 2021
I wanted to delete the high resolution chalk board style background from some lecture slides, which was singlehandledly responsible for slowing down the page render time, so I opened up the pdf file in vim.
Of course, this gave me a buffer full of binary but I soon discovered the pdf format contains some plain-text directives.
In vim I searched for
‘Image’. I found a line with /Image /Width 2048 /Height 1536
and thought,
“that looks like about the right size to be the background”,
and it was encoded in a very long stream so also has a large file size.
I deleted from the line containing 5 0 obj
until the line containing endobj
,
guessing this defined the bounds of the image object.
5 0 obj
<< /Type /XObject /Subtype /Image /Width 2048 /Height 1536 /Interpolate true
/ColorSpace 6 0 R /Intent /Perceptual /BitsPerComponent 8 /Length 616549 /Filter
/DCTDecode >>
stream
... binary soup ...
endstream
endobj
Somehow this worked, and the background disappeared, to leave the white text invisible on the white background. I had to figure out how to make the text black.
I searched for ‘color’ and found this line:
<< /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] /ColorSpace << /Cs1 6 0 R
I guessed that /Cs1 6 0 R
was the definition of the colour space, and changed 6 to 0.
Even though I have no idea what the format is, black is probably zero right?
Magically this worked, and changed all the text in the slide to black.
Then changing all the text in the document to black simply required a %s/Cs1 6/Cs1 0/g
PDF is one of those hyper-complex dark magic mysterious file formats and trying the most obvious thing and having it work the first time is something like a computer miracle.
I have not and will not check if this edit did anything meaningful, or whether the effect was purely accidental.